package com.mmall.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * Created by jia on 2018/6/9.
 */
public class DateTimeUtil {

public static final String STANDARD_FORMAT = "yyyy-MM-dd HH;mm;ss";
    //org.joda.time
    //str -> Date
    //Date -> str

    public static Date strToDate(String dateTimeStr,String formatStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
   }

   public static String dateToStr(Date date,String formarStr){
       if (date == null){
           return StringUtils.EMPTY;
       }
       DateTime dateTime = new DateTime(date);
       return dateTime.toString(formarStr);

   }

    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date){
        if (date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);

    }

    public static void main(String[] args) {
        System.out.println(new Date()); //Sun Jun 10 00:36:19 CST 2018
        System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH;mm;ss"));
        System.out.println(DateTimeUtil.strToDate("2015-06-10 00;36;19","yyyy-MM-dd HH;mm;ss"));
        //2015-06-10 00;39;15
        //Sun Jun 10 00:36:19 CST 2018


    }
}
